package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RedirectingActionForward;
import org.hibernate.Session;

import sessionfactory.HibernateSessionFactory;
public class RemoveAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception 
	{
		if (request.getParameter("delete") != null)
		{
			Session session = HibernateSessionFactory.getSessionFactory().openSession();
			String req = "DELETE FROM Produit WHERE id = " + request.getParameter("delete");
		
			int numRow = session.createQuery(req).executeUpdate();
			
			if (numRow == 0)
				return mapping.findForward("error");
			
			String referer = request.getHeader("referer");
			ActionForward forward;
			 if(referer == null)
				{
					forward = ( mapping.findForward("success"));
				}
				else
				{
					forward = new RedirectingActionForward();
					forward.setPath( referer );
				}
			//return mapping.findForward("success");
			return forward;
		}
		
		return mapping.findForward("error");
	}
}